package com.tanhua.dubbo.utils;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-12 17:14
 **/

@Component
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    public void saveTimeLine(Movement movement) {
        //2.查询当前用户的所有好友
        Long userId = movement.getUserId();
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //3.排除不可见用户，记录关系表数据
        for (Friend friend : friends) {
            MovementTimeLine line = new MovementTimeLine();
            line.setUserId(userId);
            line.setFriendId(friend.getFriendId());
            line.setCreated(System.currentTimeMillis());
            line.setMovementId(movement.getId());
            mongoTemplate.save(line);
        }

    }
}
